package com.movie.ticket.sale.movieinterface.aheadcontroller;

import com.movie.ticket.sale.movieinterface.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author： Micsal
 * @create-time： 2020.12.03 11:32
 * @version： 1.0
 * @description： 权限管理服务 包含MessageUtil和OCRUtil
 */
@RestController
@Api(tags = "权限管理(Author)", description = "用户登录，退出登录，用户注册，影院入驻，图片识别")
@RequestMapping("/author")
public class MovieAuthor {

    //========================= 手机号验证 =================================================
    @ApiOperation("获取手机验证码(4位数字)：用户登录、用户注册、影院入驻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "获取验证码的手机号", dataType = "string", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @GetMapping("/getTelCode")
    public Result getTelCode(String tel) {
        /**
         * 随机生成4位的短信验证码
         * 存入：
         * key:手机号 value:"当前时间，验证码，有效时长(分钟)"，将redis的死亡时间设置为有效时长+1，如有重复直接覆盖
         * 将key存入token，返回个客户端
         * 调用 MessageManager.sendMessage() 发送短信
         */
        return null;
    }

    @ApiOperation("校验手机验证码（4位）：统一登录、用户注册、影院入驻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "手机号", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "code", value = "短信验证码", dataType = "string", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/checkTelCode")
    public Result checkTelCode(String tel, String code) {
        /**
         * 校验手机和code是否为空
         * 校验token中的手机号是否与tel一致
         * 调用 MessageManager.checkMessage() 校验短信验证码
         */
        return null;
    }


    //======================= 登录时，随机数字验证 ===========================================================
    @ApiOperation("获取图片校验码")
    @ApiImplicitParams({})
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/getRandomNum")
    public Result getRandomNum() {
        /**
         * 获取随机数字
         * 数字的唯一性：前端重新获取数字，直接将该client的数字覆盖
         */
        return null;
    }


    @ApiOperation("验证图片校验码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "手机号", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "code", value = "数字验证码", dataType = "string", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/checkRandomNum")
    public Result checkRandomNum(String tel, String code) {
        /**
         * 校验手机和code是否为空
         * 校验token中的手机号是否与tel一致
         * 调用 MessageManager.checkMessage() 校验短信验证码
         */
        return null;
    }

    //================================== 注册时，昵称和电话实时校验 =================================
    @ApiOperation("注册时校验昵称的合法性和去重校验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "昵称，规则：abcd123456", dataType = "string", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/register/checkUserName")
    public Result registerCheckUseName(String userName) {
        /**
         * 校验昵称的唯一性
         * 校验昵称的合法性
         */
        return null;
    }


    @ApiOperation("注册时校验手机号的合法性和去重校验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "手机号", dataType = "string", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/register/checkTel")
    public Result registerCheckTel(String tel) {
        /**
         * 校验手机号的唯一性
         * 校验手机哈的合法性的初步校验
         */
        return null;
    }

    //===========================  用户注册 ============================================
    @ApiOperation("用户注册：校验短信验证码与手机号、获取密码、有用户名则录入|无则系统生成")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "手机号", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pass", value = "密码,数字+字母 6-12位", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "userName", value = "用户姓名,可为空不可为null，空时随机生成-规则：abcd123456", dataType = "string", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/user/register/checkMessage")
    public Result userRegisterCheckMessage(String tel, String pass, String userName) {
        /**
         * 校验手机号的一致性
         * 校验手机号的唯一性
         * 校验昵称的唯一性
         * 校验短信验证码
         * 校验密码是否符合规则
         * 校验用户名是否空，生成规则
         * 校验用户名和密码的敏感词/单词
         * 颁发token
         * 存登录状态（注册后自动登录）
         */
        return null;
    }

    //=============================== 登录 ==================================
    @ApiOperation("统一登录：名称/手机号+密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pass", value = "密码", dataType = "string", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/login/namePass")
    public Result loginNamePass(String name, String pass) {
        /**
         * 校验角色
         * 判断账户的状态
         * 返回角色信息
         * 跳转不同页面
         */
        return null;
    }


    @ApiOperation("统一登录：校验短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "手机号", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "string", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/login/checkTelCode")
    public Result loginCheckCode(String tel, String code) {
        /**
         * 校验手机号和验证码和时间
         * 判断账户的状态
         * 将角色信息返回前端
         * 跳转不同的页面
         */
        return null;
    }

    @ApiOperation("用户微信登录：获取微信二维码")
    @ApiImplicitParams({})
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @GetMapping("/login/wx/getQRCode")
    public Result loginWxGetQRCode() {
        /**
         * 生成微信二维码的url
         * 判断账户的状态
         * 返回给前端
         */
//        try {
//            String codeUrl = weChatService.getQRCodeUrl();
//            logger.info("codeUrl:"+codeUrl);
//            return new Message(ResponseEnum.SUCCESS,codeUrl);
//        }catch (Exception e){
//            logger.error(e.toString()+e);
//            return new Message(ResponseEnum.FALL);
//        }
        return null;
    }

    @ApiOperation("用户微信登录：微信回调验证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "微信回调验证码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "微信回调状态码", dataType = "string", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/login/wx/callBack")
    public Result loginWxCallBack(HttpServletRequest request, HttpServletResponse response, String code, String state) {
        /**
         * 验证登录状态
         * 判断账户的状态
         * 角色规定为用户
         * 返回给前端
         */
//        if(StringUtils.isBlank(code)){
//            return new Message(ResponseEnum.ESSENTIAL_IS_NULL);
//        }
//        if(StringUtils.isBlank(state)){
//            return new Message(ResponseEnum.ESSENTIAL_IS_NULL);
//        }
//        logger.info("微信回调------------");
//        logger.info(code+"------"+state);
//        try {
//            Message message=weChatService.wxCallBack(request,code, state);
//            return message;
//        }catch (Exception e){
//            logger.error(e.toString()+"\n"+e);
//            return new Message(ResponseEnum.FALL);
//        }
        return null;
    }

    //===================== 退出登录 ====================================================
    @ApiOperation("退出登录：删除redis中的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "登录者的id信息", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "role", value = "登录者的角色信息", dataType = "string", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @DeleteMapping("/logout")
    public Result logout(int id, String role) {
        /**
         * 根据id和role定位redis中信息
         * 删除指定的key值
         */
        return null;
    }

    //======================== 影院申请入驻 =======================================================
    @ApiOperation("影院申请入驻1：校验短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "申请入驻影院时，获取短信的手机号", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "code", value = "短信验证码", dataType = "string", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/cinema/apply/checkTelCode")
    public Result cinemaApplyCheckCode(String tel, String code) {
        /**
         * 0.校验是否是院线申请子影院
         * 1.校验短信验证码：
         * MessageManager.checkMessage()
         * 2.返回前端，执行下一步
         */
        return null;
    }


    @ApiOperation("影院申请入驻2：校验昵称密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "申请入驻影院时，获取短信的手机号", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pass", value = "密码", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "userName", value = "昵称", dataType = "string", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/cinema/apply/checkNameAndPass")
    public Result cinemaApplyCheckNameAndPass(String tel, String pass, String userName) {
        /**
         * 0.校验是否是院线申请子影院
         * 1.密码
         * 2.校验昵称：唯一性 合法性
         * 3.校验电话的一致性，唯一性
         * 4.返回前端，执行下一步
         */
        return null;
    }


    @ApiOperation("影院申请入驻3-完善营业信息：上传文件信息(法人身份证(2张)，营业执照(2张)),银行账户，影院名，影院级别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "文件数组，将所有的文件存入该数组", dataType = "file", paramType = "form"),
            @ApiImplicitParam(name = "account", value = "影院的银行账户", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "cinemaName", value = "影院名称", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "name", value = "法人姓名", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "identity", value = "法人身份证明", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "degree", value = "影院等级", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "registerNum", value = "营业执照的注册号或统一社会信用代码", dataType = "string", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/cinema/apply/updateLegalFile")
    public Result cinemaApplyUpdateLegalFile(MultipartFile[] files, String account,String cinemaName, String name, String identity, int degree, String registerNum) {
        /**
         * 0.校验是否是院线申请子影院
         * 调用图片识别，验证文件的清晰程度
         * 校验图片与上传信息的一致性
         * *校验营业执照的姓名和身份证的身份的一致性
         * 校验影院名和级别
         */
        return null;
    }

    @ApiOperation("影院申请入驻4-完善影院信息：上传文件信息(影院封面(1张)，影院详情(多张)),字符串地址，经纬度，支持的服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "文件数组，将所有的文件存入该数组", dataType = "file", paramType = "form"),
            @ApiImplicitParam(name = "addr", value = "影院地址", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "longitude", value = "维度", dataType = "double", paramType = "form"),
            @ApiImplicitParam(name = "latitude", value = "经度", dataType = "double", paramType = "form"),
            @ApiImplicitParam(name = "service", value = "影院支持的服务", dataType = "string", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/cinema/apply/updateCinemaFile")
    public Result cinemaApplyUpdateCinemaFile(MultipartFile[] files, String addr, double longitude, double latitude, String  service) {
        /**
         * 0.校验是否是院线申请子影院
         * 1.存储信息
         * 2.如果是申请子影院-到这一步申请结束-跳转到展示请等待人工审核的界面
         */
        return null;
    }

    private static final String DEFAULT_CONTRACT = "影院的默认合同地址";
    @ApiOperation("影院申请入驻5：影院，拉取默认合同(word文档)")
    @ApiImplicitParams({})
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回文件中的信息，map"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @GetMapping("/cinema/apply/getDefaultContract")
    public Result cinemaApplyGetDefaultContract() {
        /**
         * 获取文件-默认合同
         * 解析文件
         * 生成新的合同编号
         * 文件的信息： 存入map传给client
         *      合同编号
         *      合同版本-默认V1.0
         *      甲方
         *      乙方
         *      年费
         *      平台手续费
         *      时间
         *      审核状态：“待审核”，“驳回”，“同意”
         * 将文件存入cinemaData文件夹中
         */
        return null;
    }


    @ApiOperation("影院申请入驻6：影院，申请修改默认合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractNum", value = "合同编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "annualCost", value = "年费", dataType = "double", paramType = "query"),
            @ApiImplicitParam(name = "tipRoot", value = "手续费", dataType = "double", paramType = "query"),
            @ApiImplicitParam(name = "changes", value = "第一位:年费是否申请改变，第二位：手续费是否申请改变", dataType = "boolean[]", paramType = "query")

    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/cinema/apply/changeContract")
    public Result cinemaApplyChangeContract(String contractNum,double annualCost,double tipRoot,boolean[] changes) {
        /**
         * 修改合同信息
         * 拉取cinemaData中的信息
         * 复写合同
         * 存入
         * 等待审核
         */
        return null;
    }


    @ApiOperation("影院申请入驻7：影院，确认签约")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "signImg", value = "乙方签名", dataType = "file", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/cinema/apply/configContract")
    public Result cinemaApplyConfigContract(MultipartFile signImg) {
        /**
         * 将图片、签约时间存入word合同
         * 调用MessageManager发送："尊敬的xxxx，你在微票视界上申请入驻影院(影院名-地址)，正在审核，请耐心等待。"
         */
        return null;
    }

    @ApiOperation("影院申请入驻8：院线，工作人员上传合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contract", value = "院线的合同", dataType = "file", paramType = "form"),
            @ApiImplicitParam(name = "name", value = "申请法人姓名", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "tel", value = "申请法人电话", dataType = "string", paramType = "form")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/cinema/apply/updateContract")
    public Result cinemaApplyUpdateContract(MultipartFile contract,String name,String tel) {
        /**
         * 查找影院
         * 将影院的签约的合同上传
         */
        return null;
    }

    @ApiOperation("影院申请入驻9：院线申请入驻，申请成功发送短信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "申请法人电话", dataType = "file", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = Result.class, message = "成功,返回Result.success"),
            @ApiResponse(code = 426, response = Result.class, message = "失败，信息存result.message中")
    })
    @PostMapping("/cinema/apply/sendMessage")
    public Result cinemaApplySendMessage(String tel) {
        /**
         * 查找影院
         * 调用MessageManager发送："尊敬的xxxx，你在微票视界上申请入驻院线(xxxx影城)，正在审核，请耐心等待。"
         */
        return null;
    }






}
